Passed
Push — master ( 7e3e50...4e9cd3 )
by Michael
02:46
created

script.js ➔ ... ➔ jQuery.fail   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 3
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
/**
2
 * Hide Prosemirror and show the default editor
3
 *
4
 * @param {string} text the wiki syntax to be shown in the textarea
5
 */
6
function showDefaultEditor(text) {
7
    window.Prosemirror.destroyProsemirror();
8
    window.proseMirrorIsActive = false;
9
    dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft);
0 ignored issues
show
Bug introduced by
The variable dw_locktimer seems to be never declared. If this is a global, consider adding a /** global: dw_locktimer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
    jQuery('#wiki__text').val(text).show();
11
    jQuery('#size__ctl').show();
12
    jQuery('.editBox > .toolbar').show();
13
}
14
15
/**
16
 * Hide the default editor and start a new Prosemirror Editor
17
 *
18
 * @param {string} json the prosemirror document json
19
 */
20
function showProsemirror(json) {
21
    const $textArea = jQuery('#wiki__text');
22
    const $prosemirrorJsonInput = jQuery('#dw__editform').find('[name=prosemirror_json]').val(json);
23
    try {
24
        window.Prosemirror.enableProsemirror();
25
    } catch (e) {
26
        console.error(e);
27
        let message = 'There was an error in the WYSIWYG editor. You will be redirected to the syntax editor in 5 seconds.';
28
        if (window.SentryPlugin) {
29
            SentryPlugin.logSentryException(e, {
0 ignored issues
show
Bug introduced by
The variable SentryPlugin seems to be never declared. If this is a global, consider adding a /** global: SentryPlugin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
30
                tags: {
31
                    plugin: 'prosemirror',
32
                    'id': JSINFO.id,
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
33
                },
34
                extra: {
35
                    'content': $textArea.val(),
36
                    'json': $prosemirrorJsonInput.val(),
37
                }
38
            });
39
            message += ' -- The error has been logged to Sentry.';
40
        }
41
        showErrorMessage(message);
42
        setTimeout(function() {
43
            jQuery('.plugin_prosemirror_useWYSIWYG').click();
44
        }, 5000);
45
    }
46
    window.proseMirrorIsActive = true;
47
    $textArea.hide();
48
    jQuery('#size__ctl').hide();
49
    jQuery('.editBox > .toolbar').hide();
50
51
    if (dw_locktimer.addField) {
0 ignored issues
show
Bug introduced by
The variable dw_locktimer seems to be never declared. If this is a global, consider adding a /** global: dw_locktimer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
        // todo remove this guard after the next stable DokuWiki release after Greebo
53
        dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft, 'prosemirror__editor');
54
        dw_locktimer.addField('input[name=prosemirror_json]');
55
    } else {
56
        console.warn('Draft saving in WYSIWYG is not available. Please upgrade your wiki to the current development snapshot.')
57
    }
58
}
59
60
/**
61
 * Initialize the prosemirror framework
62
 *
63
 * (This shouldn't do much until we actually use the editor, but we maybe shouldn't do this twice)
64
 */
65
function initializeProsemirror() {
66
    try {
0 ignored issues
show
Unused Code introduced by
Empty try statement found. This statement can be removed.
Loading history...
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
67
        /* DOKUWIKI:include lib/bundle.js */
68
    } catch (e) {
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
69
        const $textArea = jQuery('#wiki__text');
0 ignored issues
show
Bug introduced by
The variable $textArea seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$textArea.
Loading history...
70
        console.error(e);
71
        let message = 'There was an error initializing the WYSIWYG editor.';
0 ignored issues
show
Bug introduced by
The variable message seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.message.
Loading history...
72
        if (window.SentryPlugin) {
73
            SentryPlugin.logSentryException(e, {
74
                tags: {
75
                    plugin: 'prosemirror',
76
                    'id': JSINFO.id,
77
                },
78
                extra: {
79
                    'content': $textArea.val(),
80
                }
81
            });
82
            message += ' The error has been logged to sentry.';
83
        }
84
85
        showErrorMessage(message);
86
87
        DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', '');
88
    }
89
}
90
91
/**
92
 * Add the error message above the editor
93
 *
94
 * @param {string} errorMsg
95
 */
96
function showErrorMessage(errorMsg) {
97
    jQuery('.editBox').before(
98
        jQuery('<div class="error"></div>').text(errorMsg)
99
    );
100
}
101
102
/**
103
 * Switch between WYSIWYG and Syntax editor
104
 */
105
function toggleEditor() {
106
    const $textArea = jQuery('#wiki__text');
107
    const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
108
    jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', {
0 ignored issues
show
Bug introduced by
The variable DOKU_BASE seems to be never declared. If this is a global, consider adding a /** global: DOKU_BASE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
109
        call: 'plugin_prosemirror_switch_editors',
110
        data: window.proseMirrorIsActive ? $jsonField.val() : $textArea.val(),
111
        getJSON: window.proseMirrorIsActive ? '0' : '1',
112
    }).done(function handleSwitchEditorResponse(data) {
113
        if (window.proseMirrorIsActive) {
114
            showDefaultEditor(data.text);
115
        } else {
116
            showProsemirror(data.json);
117
        }
118
    }).fail(function (jqXHR, textStatus, errorThrown) {
119
        console.error(jqXHR, textStatus, errorThrown); // FIXME: proper error handling
120
        if (jqXHR.responseJSON && jqXHR.responseJSON.error) {
121
            showErrorMessage(jqXHR.responseJSON.error);
122
        } else {
123
            let message = 'The request failed with an unexpected error.';
124
            if (window.SentryPlugin) {
125
                SentryPlugin.logSentryException(new Error(textStatus), {
0 ignored issues
show
Bug introduced by
The variable SentryPlugin seems to be never declared. If this is a global, consider adding a /** global: SentryPlugin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
126
                    tags: {
127
                        plugin: 'prosemirror',
128
                        'id': JSINFO.id,
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
129
                        status: jqXHR.status
130
                    },
131
                    extra: {
132
                        'content': $textArea.val(),
133
                        'responseText': jqXHR.responseText,
134
                    }
135
                });
136
                message += ' -- The error has been logged to Sentry.';
137
            }
138
            showErrorMessage(message);
139
        }
140
    });
141
142
    const $current = DokuCookie.getValue('plugin_prosemirror_useWYSIWYG');
0 ignored issues
show
Bug introduced by
The variable DokuCookie seems to be never declared. If this is a global, consider adding a /** global: DokuCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
143
    DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', $current ? '' : '1');
144
}
145
146
/**
147
 * If the cookie is set, then show the WYSIWYG editor and add the switch-editor-event to the button
148
 */
149
function handleEditSession() {
150
    const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
151
    if (DokuCookie.getValue('plugin_prosemirror_useWYSIWYG')) {
0 ignored issues
show
Bug introduced by
The variable DokuCookie seems to be never declared. If this is a global, consider adding a /** global: DokuCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
152
        showProsemirror($jsonField.val());
153
    }
154
    const $toggleEditorButton = jQuery('.plugin_prosemirror_useWYSIWYG');
155
    $toggleEditorButton.on('click', toggleEditor);
156
}
157
158
jQuery(function () {
159
    window.proseMirrorIsActive = false;
160
161
    initializeProsemirror();
162
163
    if (jQuery('#dw__editform').find('[name=prosemirror_json]').length) {
164
        handleEditSession();
165
    }
166
167
    jQuery(window).on('fastwiki:afterSwitch', function(evt, viewMode, isSectionEdit, prevViewMode) {
0 ignored issues
show
Unused Code introduced by
The parameter prevViewMode is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
168
        if (viewMode === 'edit' || isSectionEdit) {
169
            handleEditSession();
170
        }
171
    });
172
});
173